package com.example.androidshixun.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.androidshixun.LoginActivity;
import com.example.androidshixun.MainActivity;
import com.example.androidshixun.R;
import com.example.androidshixun.utils.PreferenceUtils;
import com.google.android.material.snackbar.Snackbar;

public class Tab4Fragment extends Fragment implements View.OnClickListener {

    private TextView userName;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_4_layout, container, false);
        RelativeLayout info = view.findViewById(R.id.user_info);
        LinearLayout setting = view.findViewById(R.id.setting);
        userName = view.findViewById(R.id.user_name);

        info.setOnClickListener(this);
        setting.setOnClickListener(this);

        if(  PreferenceUtils.getInstance(getContext()).readLoginStatus()  ) {
            userName.setText(PreferenceUtils.getInstance(getContext()).readUserName());
        } else {
            userName.setText("点击登录");
        }
        return view;
    }

    public void setUserNameTv(String what) {
        if( userName != null) {
            userName.setText(what);
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting:
                if(  PreferenceUtils.getInstance(getContext()).readLoginStatus()  ) {
                    //TODO 已经登录了  跳转到设置页面
                } else {
                    Snackbar.make(view,"请登录", Snackbar.LENGTH_SHORT).show();
                }
                break;
            case R.id.user_info:
                if(  PreferenceUtils.getInstance(getContext()).readLoginStatus()  ) {
                    //TODO 已经登录了  跳转到个人资料页面
                } else {
                    //TODO 未登录 跳转到登录页面
                    Intent intent = new Intent( getContext() , LoginActivity.class);
                    getActivity().startActivityForResult(intent, MainActivity.requestLogin);
                }
                break;
        }
    }
}
